﻿/*
 * Copyright (C) 2010 Jakub Miara
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;

namespace Net.Araim.FavouriteLocations
{
    public class SortableBindingList<T> : BindingList<T>, IRaiseItemChangedEvents
    {

        public SortableBindingList(List<T> l) : base(l) { }

        protected override bool SupportsSearchingCore
        {
            get
            {
                return true;
            }
        }

        protected override bool SupportsSortingCore
        {
            get
            {
                return true;
            }
        }



        protected override void ApplySortCore(PropertyDescriptor property, ListSortDirection direction)
        {
            List<T> itemsList = (List<T>)this.Items;


            if (property.PropertyType.GetInterface("IComparable") != null)
            {
                itemsList.Sort(new Comparison<T>(delegate(T x, T y)
                {
                    if (property.GetValue(x) != null)
                    {
                        if (property.GetValue(x) is string)
                        {
                            return string.Compare((property.GetValue(x) as string), (property.GetValue(y) as string), true) * (direction == ListSortDirection.Descending ? -1 : 1);
                        }
                        else
                        {
                            return ((IComparable)property.GetValue(x)).CompareTo(property.GetValue(y)) * (direction == ListSortDirection.Descending ? -1 : 1);
                        }
                    }
                    else if (property.GetValue(y) != null)
                    {
                        if (property.GetValue(x) is string)
                        {
                            return string.Compare((property.GetValue(x) as string), (property.GetValue(y) as string), true) * (direction == ListSortDirection.Descending ? -1 : 1);
                        }
                        else
                        {
                            return ((IComparable)property.GetValue(y)).CompareTo(property.GetValue(x)) * (direction == ListSortDirection.Descending ? 1 : -1);
                        }
                    }
                    else
                    {
                        return 0;
                    }
                }));
            }
        }

    }
}
